package top.zhangjianyong.utils;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author zhangjianyong
 * @since 2024/11/14 上午9:29
 */
@Slf4j
public class PDFUtils {

    public static String readPDF(String path) throws IOException {
        StringBuilder sb = new StringBuilder();
        PdfReader pdfReader = new PdfReader(path);
        try {
            int size = pdfReader.getNumberOfPages();
            for (int i = 1; i < size + 1; ) {
                //一页页读取PDF文本
                String pageStr = PdfTextExtractor.getTextFromPage(pdfReader, i);
                sb.append(pageStr).append("\n").append("PDF解析第").append(i).append("页\n");
                i = i + 1;
            }
        } finally {
            pdfReader.close();
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        String path = "/Users/zhangjianyong/Library/Mobile Documents/com~apple~CloudDocs/Downloads/98128227419018_NORMAL_COLOR_20241111124516568.pdf";

        String s = readPDF(path);
        System.out.println(s);

    }
}
